package com.sysmanage.permissions.service.impl;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sysmanage.common.tools.page.Pagination;
import com.sysmanage.framework.dao.hspring.DAOInterface;
import com.sysmanage.framework.service.BaseService;
import com.sysmanage.permissions.dao.CompanyDao;
import com.sysmanage.permissions.dao.DepartmentDao;
import com.sysmanage.permissions.dao.UsersDao;
import com.sysmanage.permissions.model.CloneUsers;
import com.sysmanage.permissions.model.Company;
import com.sysmanage.permissions.model.Department;
import com.sysmanage.permissions.model.Users;
import com.sysmanage.permissions.service.ILogService;
import com.sysmanage.permissions.service.IUserService;

@Service
@Transactional
public class UserService extends BaseService<Users> implements IUserService {
	@Autowired
	private UsersDao userDao;
	
	@Resource(name="departmentDao")
	private DepartmentDao departmentDao;
	
	@Resource(name="companyDao")
	private CompanyDao companyDao;
	
	@Resource(name="logService")
	private ILogService logService;
	
	@Override
	protected DAOInterface<Users> getDAO() {
		return userDao;
	}
	
	public String returnRoleName(String userId){
		return userDao.returnRoleName(userId);
	}

	public List<Users> getAll() {
		return userDao.getAll();
	}

	public UsersDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UsersDao userDao) {
		this.userDao = userDao;
	}
	/**
	 * 获取部门用户树
	 * @param 部门ID
	 * @return
	 */
	
	public List<Map<String,Object>> getDeptUserTree(String deptId)
	{
		return this.userDao.getDeptUserTree(deptId);
	}
	public List<Users> getGroupAll(Integer groupId) {
		return this.userDao.getGroupAll(groupId);
	}
	public Pagination getGroupAllPageList(Integer groupId, int page, int rows,String sort,String order) {
		return this.userDao.getGroupAllPageList(groupId, page, rows,sort,order);
	}
	public Pagination getPageList(Users sys, int page, int rows,String sort,String order) {
		return this.userDao.getPageList(sys, page, rows,sort,order);
	}
	/** 
	  * @Title: getGroupOtherPageList 
	  * @Description: 具体某页显示多少条记录
	  * @param @param groupId 组id
	  * @param @param user 一条具体的用户信息
	  * @param @param page 当前页
	  * @param @param rows 一页显示多少条记录
	  * @param @return
	  * @return Pagination
	  * @throws 
	  */
	public Pagination getGroupOtherPageList(Integer groupId,Users user, int page, int rows,String sort,String order)
	{
		return userDao.getGroupOtherPageList(groupId, user, page, rows, sort, order);
	}
	/** 
	  * @Title: getAllValidUser 
	  * @Description: 获取所有有用的用户信息
	  * @param @return
	  * @return List<Users>
	  * @throws 
	  */
	public List<Users> getAllValidUser()
	{
		return this.userDao.getAllValidUser();
	}
	public Boolean saveToGroup(String groupId, Users user) {

		boolean ret = this.userDao.saveToGroup(groupId, user);
		return ret;
	}

	public Users login(String userName, String password) {
		return this.userDao.login(userName, password);
	}

	public boolean alterToGroup(Users user, String groupId) {
		return this.userDao.alterToGroup(user,groupId);
	}
	public List<Users> findByLoginIds(String ids){
		return this.userDao.findByLoginIds(ids);
	}
	public List<Users> findByName(String name){
		return this.userDao.findByName(name);
	}
	public boolean deleteUserById(Integer id)
	{
		boolean ret = false;
		Users user = userDao.findById(id);
		if(user!=null)
		{			
			ret =  this.userDao.deleteById(id);
			logService.log("删除用户"+user.getName()+"("+user.getLogonid()+")及子帐号成功！", "同步");
		}
		return ret;
	}
	public boolean saveUser(Users user)
	{
		boolean ret = true;		
		ret =  this.userDao.save(user);		
		return ret;
	}
	
	public List<CloneUsers> convert(List<Users> users,boolean showPS){
		List<CloneUsers> clones = new ArrayList<CloneUsers>();
		try {
			
			if(users!=null && users.size()!=0){
				Department tmpDept = null;
				Users tmpUser = null;
				for(Iterator<Users> iter=users.iterator();iter.hasNext();){
					Users res = iter.next();
					CloneUsers c = new CloneUsers();
					BeanUtils.copyProperties(c, res);
					//todo
					/*
					if(!showPS)
						c.setPassword(ConstCode.SHOW_PASSW0RD);  //不显示真实密码
					else
						c.setPassword(enc.desString(res.getPassword())); //解密
					*/
					
					if(res.getDeptId()!=null){
						tmpDept = departmentDao.findById(res.getDeptId());
						if(tmpDept!=null)
							c.setDeptName(tmpDept.getDeptName());
					}
					if(res.getManagerId()!=null){
						tmpUser = userDao.findById(res.getManagerId());
						if(tmpUser!=null)
							c.setManagerName(tmpUser.getName());
					}
					//翻译公司
					if(res.getCompanyId()!=null){
						Company tmpCompany = companyDao.findById(res.getCompanyId());
						if(tmpCompany!=null)
							c.setCompanyName(tmpCompany.getCompanyName());

					}
					/*
					//翻译客户类型					
					if(res.getExt1()!=null&&res.getExt1().trim().length()>0){					
						
						tmpStr = res.getExt1().trim();
						String[] custType = tmpStr.split(",");
						tmpStr = null;
						DictionaryMemoryUtils dictionaryUtils = DictionaryMemoryUtils.getInstance();
						Map<String,String> maps = dictionaryUtils.getDictionarysMap().get(ConstCode.DIC_NICK_NAME_REGUSERTYPE);
						for(int i=0;i<custType.length;i++){
							if(tmpStr==null)
							{
								tmpStr = maps.get(custType[i]);
							}
							else
							{
								tmpStr = tmpStr + "," + maps.get(custType[i]);
							}
						}
						c.setCustTypeName(tmpStr);
						
					}*/
					clones.add(c);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return clones;
	}
	public Users convert(CloneUsers user){
		Users res = new Users();
		try {
			BeanUtils.copyProperties(res,user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
	}

}
